Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace Data
    ''' <summary>
    ''' Controller class for Messages
    ''' </summary>
		<System.ComponentModel.DataObject()> Public Partial Class MessagesController
    
        ' Preload our schema..
        Dim thisSchemaLoad As Messages = New Messages()
        Private strUserName As String = String.Empty
        Protected ReadOnly Property UserName() As String
            Get
				If strUserName.Length = 0 Then
		        
    				If Not System.Web.HttpContext.Current Is Nothing Then
						strUserName = System.Web.HttpContext.Current.User.Identity.Name
					Else
		        		strUserName = System.Threading.Thread.CurrentPrincipal.Identity.Name
					End If
					Return strUserName
				End If
				Return strUserName
			End Get
        End Property
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchAll() As MessagesCollection
        
            Dim coll As MessagesCollection = New MessagesCollection()
            Dim qry As Query = New Query(Messages.Schema)
            coll.LoadAndCloseReader(qry.ExecuteReader())
            Return coll
            
        End Function
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchByID(ByVal Id As Object) As MessagesCollection 
        
            Dim coll As MessagesCollection = New MessagesCollection().Where("id", Id).Load()
            Return coll
        
        End Function
        
        <DataObjectMethod(DataObjectMethodType.Select, True)> Public Function FetchByQuery(ByVal qry As SubSonic.Query) As MessagesCollection 
        
            Dim coll As MessagesCollection = New MessagesCollection()
            coll.LoadAndCloseReader(qry.ExecuteReader())
            Return coll
        
        End Function
        <DataObjectMethod(DataObjectMethodType.Delete, True)> Public Function Delete(ByVal Id As Object) as Boolean
        
            Return (Messages.Delete(Id) = 1)
        
        End Function
        <DataObjectMethod(DataObjectMethodType.Delete, False)> Public Function Destroy(ByVal Id As Object) as Boolean
        
            Return (Messages.Destroy(Id) = 1)
        
        End Function
        
    	
	    ''' <summary>
	    ''' Inserts a record, can be used with the Object Data Source
	    ''' </summary>
        <DataObjectMethod(DataObjectMethodType.Insert, True)> Public Sub Insert(ByVal Memberid As Guid,ByVal Postdate As DateTime,ByVal Subject As String,ByVal Body As String,ByVal Threadid As Integer,ByVal Parentmsgid As Integer,ByVal Answer As Boolean)
	   
		    Dim item As Messages = New Messages()
		    
            item.Memberid = Memberid
            
            item.Postdate = Postdate
            
            item.Subject = Subject
            
            item.Body = Body
            
            item.Threadid = Threadid
            
            item.Parentmsgid = Parentmsgid
            
            item.Answer = Answer
            
	    
		    item.Save(UserName)
	   
	   End Sub
    	
	    ''' <summary>
	    ''' Updates a record, can be used with the Object Data Source
	    ''' </summary>
        <DataObjectMethod(DataObjectMethodType.Update, True)> Public Sub Update(ByVal Id As Integer,ByVal Memberid As Guid,ByVal Postdate As DateTime,ByVal Subject As String,ByVal Body As String,ByVal Threadid As Integer,ByVal Parentmsgid As Integer,ByVal Answer As Boolean)
	    
		    Dim item As Messages = New Messages()
		    
				item.Id = Id
				
				item.Memberid = Memberid
				
				item.Postdate = Postdate
				
				item.Subject = Subject
				
				item.Body = Body
				
				item.Threadid = Threadid
				
				item.Parentmsgid = Parentmsgid
				
				item.Answer = Answer
				
		    item.MarkOld()
		    item.Save(UserName)
	    
	    End Sub
    End Class
End Namespace
